package com.quanyan.place.component;

import com.quanyan.mq.client.QcMsgClient;
import com.quanyan.mq.client.reqeust.MqRequestMsg;
import com.quanyan.mq.client.reqeust.SendCallbackListener;
import com.quanyan.mq.client.response.MqResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;

/**
 * Created by Kingson.chan on 2016/8/10 0010.
 */
@Component
public class MqPlaceProducerClientImpl implements QcMsgClient {

    private final static Logger logger = LoggerFactory.getLogger(MqPlaceProducerClientImpl.class);

    @Autowired
    private MqPlaceProducer mqPlaceProducer;

    @Nonnull
    @Override
    public MqResponse syncSend(@Nonnull MqRequestMsg mqRequestMsg) {
        MqResponse mqResponse = this.mqPlaceProducer.syncSend(mqRequestMsg);
        return mqResponse;
    }

    @Override
    public void asyncSend(@Nonnull MqRequestMsg mqRequestMsg) {
        //logger.warn("SEND TO MQ : DATA : {}", new Gson().toJson(mqRequestMsg).toString());
        mqPlaceProducer.asyncSend(mqRequestMsg, new SendCallbackListener() {
            @Override
            public void onSuccess(String sendResult) {
                logger.warn("SEND TO MQ : RESULT : {}", sendResult);
            }

            @Override
            public void onException(Throwable e) {
                logger.warn("SEND TO MQ EXCEPTION : {}", e.getMessage());
            }
        });
    }
}
